import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ProjectSourceCodeStatistics {

    // 计算文件的字节数
    public static long calculateByteCount(File file) {
        if (!file.exists() || !file.isFile()) {
            return 0;
        }
        return file.length();
    }

    // 计算文件的行数
    public static int calculateLineCount(File file) {
        int lineCount = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            while (reader.readLine() != null) {
                lineCount++;
            }
        } catch (IOException e) {
            System.out.println("读取文件 " + file.getAbsolutePath() + " 时发生错误: " + e.getMessage());
        }
        return lineCount;
    }

    // 递归遍历文件夹并统计字节数和行数
    public static void traverseFolder(File folder, long[] byteCount, int[] lineCount) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        traverseFolder(file, byteCount, lineCount);
                    } else {
                        byteCount[0] += calculateByteCount(file);
                        lineCount[0] += calculateLineCount(file);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        String folderPath = "D:\\future\\course\\JAVA\\IDEA\\Treasure-Pavilion-Admin\\src\\main\\java\\com\\yang";
        File projectFolder = new File(folderPath);

        long[] totalByteCount = {0};
        int[] totalLineCount = {0};

        traverseFolder(projectFolder, totalByteCount, totalLineCount);

        System.out.println("项目源程序的总字节数: " + totalByteCount[0]);
        System.out.println("项目源程序的总行数: " + totalLineCount[0]);
    }
}    